package jnpf.service.impl;

import jnpf.entity.Whu_commuityEntity;
import jnpf.mapper.Whu_commuityMapper;
import jnpf.service.Whu_commuityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import jnpf.model.whu_commuity.Whu_commuityPagination;


import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jnpf.util.*;
import java.util.*;

/**
 *
 * whu_commuity
 * 版本： V3.2.0
 * 版权： 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * 作者： JNPF开发平台组
 * 日期： 2025-07-05
 */
@Service

public class Whu_commuityServiceImpl extends ServiceImpl<Whu_commuityMapper, Whu_commuityEntity> implements Whu_commuityService {

    @Autowired
    private UserProvider userProvider;






    @Override
    public List<Whu_commuityEntity> getList(Whu_commuityPagination whu_commuityPagination){
        String userId=userProvider.get().getUserId();
        List<String> AllIdList =new ArrayList();
        int total=0;
        QueryWrapper<Whu_commuityEntity> whu_commuityQueryWrapper=new QueryWrapper<>();

        if (StringUtil.isNotEmpty(whu_commuityPagination.getUserid())) {
            whu_commuityQueryWrapper.lambda().like(Whu_commuityEntity::getUserid, userId);
        }

        if (StringUtil.isNotEmpty(whu_commuityPagination.getTitle())
                || StringUtil.isNotEmpty(whu_commuityPagination.getContent())) {

            whu_commuityQueryWrapper.and(wrapper -> {
                if (StringUtil.isNotEmpty(whu_commuityPagination.getTitle())) {
                    wrapper.lambda().like(Whu_commuityEntity::getTitle, whu_commuityPagination.getTitle());
                }
                if (StringUtil.isNotEmpty(whu_commuityPagination.getContent())) {
                    if (StringUtil.isNotEmpty(whu_commuityPagination.getTitle())) {
                        wrapper.or();
                    }
                    wrapper.lambda().like(Whu_commuityEntity::getContent, whu_commuityPagination.getContent());
                }
            });
        }

        if(StringUtil.isNotEmpty(whu_commuityPagination.getTags())){
            whu_commuityQueryWrapper.lambda().like(Whu_commuityEntity::getTags,whu_commuityPagination.getTags());
        }

        if(AllIdList.size()>0){
            whu_commuityQueryWrapper.lambda().in(Whu_commuityEntity::getId, AllIdList);
        }
        //排序
        if(StringUtil.isEmpty(whu_commuityPagination.getSidx())){
        whu_commuityQueryWrapper.lambda().orderByDesc(Whu_commuityEntity::getId);
        }else{
        whu_commuityQueryWrapper="asc".equals(whu_commuityPagination.getSort().toLowerCase())?whu_commuityQueryWrapper.orderByAsc(whu_commuityPagination.getSidx()):whu_commuityQueryWrapper.orderByDesc(whu_commuityPagination.getSidx());
        }
        if((total>0 && AllIdList.size()>0) || total==0){
        Page<Whu_commuityEntity> page=new Page<>(whu_commuityPagination.getCurrentPage(), whu_commuityPagination.getPageSize());
        IPage<Whu_commuityEntity> userIPage=this.page(page, whu_commuityQueryWrapper);
        return whu_commuityPagination.setData(userIPage.getRecords(),userIPage.getTotal());
        }else{
            List<Whu_commuityEntity> list = new ArrayList();
            return whu_commuityPagination.setData(list, list.size());
        }
    }
    @Override
    public List<Whu_commuityEntity> getTypeList(Whu_commuityPagination whu_commuityPagination,String dataType){
        String userId=userProvider.get().getUserId();
        List<String> AllIdList =new ArrayList();
		int total=0;
                    QueryWrapper<Whu_commuityEntity> whu_commuityQueryWrapper=new QueryWrapper<>();
            int whu_commuityNum =0;

            whu_commuityQueryWrapper.lambda().like(Whu_commuityEntity::getUserid,userId);

			if(StringUtil.isNotEmpty(whu_commuityPagination.getTitle())){
            whu_commuityNum++;
            whu_commuityQueryWrapper.lambda().like(Whu_commuityEntity::getTitle,whu_commuityPagination.getTitle());
                    }

			if(StringUtil.isNotEmpty(whu_commuityPagination.getContent())){
            whu_commuityNum++;
            whu_commuityQueryWrapper.lambda().like(Whu_commuityEntity::getContent,whu_commuityPagination.getContent());
                    }

			if(StringUtil.isNotEmpty(whu_commuityPagination.getTags())){
            whu_commuityNum++;
            whu_commuityQueryWrapper.lambda().like(Whu_commuityEntity::getTags,whu_commuityPagination.getTags());
                    }

		if(AllIdList.size()>0){
        whu_commuityQueryWrapper.lambda().in(Whu_commuityEntity::getId, AllIdList);
				}
        //排序
        if(StringUtil.isEmpty(whu_commuityPagination.getSidx())){
        whu_commuityQueryWrapper.lambda().orderByDesc(Whu_commuityEntity::getId);
        }else{
        whu_commuityQueryWrapper="asc".equals(whu_commuityPagination.getSort().toLowerCase())?whu_commuityQueryWrapper.orderByAsc(whu_commuityPagination.getSidx()):whu_commuityQueryWrapper.orderByDesc(whu_commuityPagination.getSidx());
        }
        if("0".equals(dataType)){
            if((total>0 && AllIdList.size()>0) || total==0){
                Page<Whu_commuityEntity> page=new Page<>(whu_commuityPagination.getCurrentPage(), whu_commuityPagination.getPageSize());
                IPage<Whu_commuityEntity> userIPage=this.page(page, whu_commuityQueryWrapper);
                return whu_commuityPagination.setData(userIPage.getRecords(),userIPage.getTotal());
            }else{
                List<Whu_commuityEntity> list = new ArrayList();
                return whu_commuityPagination.setData(list, list.size());
            }
        }else{
            return this.list(whu_commuityQueryWrapper);
        }
    }

    @Override
    public Whu_commuityEntity getInfo(String id){
        QueryWrapper<Whu_commuityEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(Whu_commuityEntity::getId,id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(Whu_commuityEntity entity){
        this.save(entity);
    }

    @Override
    public boolean update(String id, Whu_commuityEntity entity){
        entity.setId(id);
        return this.updateById(entity);
    }
    @Override
    public void delete(Whu_commuityEntity entity){
        if(entity!=null){
            this.removeById(entity.getId());
        }
    }
    //子表方法

    //列表子表数据方法

}
